iT邦幫忙

2024 iThome 鐵人賽

DAY 29
0
  1. 先在根目錄建立 modules/功能名稱 資料夾。

  2. 把 Module 及相關原生程式碼檔案移至這個資料夾。

  3. 建立 index.jspackage.json 檔案。

  4. First, let's modify your index.js file in the modules/notification_progress directory to add some debug logging:

import { NativeModulesProxy, EventEmitter } from 'expo-modules-core';

console.log('NativeModulesProxy:', NativeModulesProxy);
console.log('NotificationProgressModule:', NativeModulesProxy.NotificationProgressModule);

const NotificationProgressModule = NativeModulesProxy.NotificationProgressModule;

export function buildNotification() {
  console.log('Attempting to call buildNotification');
  if (NotificationProgressModule && NotificationProgressModule.buildNotification) {
    return NotificationProgressModule.buildNotification();
  } else {
    console.error('buildNotification is not available');
    return 'Error: buildNotification is not available';
  }
}

export default NotificationProgressModule;

  1. Make sure your Kotlin file (NotificationProgressModule.kt) is correctly implementing the function:
package expo.modules.notificationprogress

import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition

class NotificationProgressModule : Module() {
  override fun definition() = ModuleDefinition {
    Name("NotificationProgressModule")

    Function("buildNotification") {
      // Your implementation here
      "Notification built from Kotlin!" // Return some value
    }
  }
}

  1. In your main app code where you're trying to use the module, modify it to include error handling:
import { buildNotification } from 'notification_progress';

try {
  console.log('Imported module:', buildNotification);
  const result = buildNotification();
  console.log('Result:', result);
} catch (error) {
  console.error('Error calling buildNotification:', error);
}
  1. Make sure your app.json or app.config.js is correctly referencing the module:
{
  "expo": {
    "plugins": [
      "modules/notification_progress"
    ]
  }
}
  1. In your project root, run:
npx expo prebuild --clean
  1. After prebuild, check the generated Android project. Look in android/app/src/main/java/com/your_project_name/MainApplication.java (or .kt if it's Kotlin). Make sure there's a line adding your package to the list of packages:
List<ReactPackage> packages = new PackageList(this).getPackages();
// ... potentially other packages
packages.add(new expo.modules.notificationprogress.NotificationProgressPackage());

If this line is missing, Expo might not be recognizing your module correctly.

  1. Rebuild and run your project:
npx expo run:android
  1. Check the Metro bundler console and your app's logs for any error messages or the debug output we added.

If you're still encountering issues, it could be due to a few reasons:

  • The native module is not being compiled or linked correctly.
  • There's a mismatch between the JavaScript interface and the native implementation.
  • The Expo config is not correctly set up to include your module.

At this point, it would be helpful to see:

  1. The complete error message and stack trace from your app.
  2. The output of the debug logs we added.
  3. The contents of your android/settings.gradle and android/app/build.gradle files.

With this information, we can better diagnose why the module isn't being recognized properly.


上一篇
[Day 28] 解決 Module 未能初始化問題-續
下一篇
[Day 30] 如何套用 Module ?
系列文
跨平台協同:在 React Native 和 Kotlin 應用中實現無縫交互 -以 Notification 為例30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言